/*
 * HeapGUI.cpp
 *
 *  Created on: 13-Dec-2009
 *      Author: swapnil
 */
#include "../DiskSpaceManager/Database.h"
#include <iostream>
#include <stdio.h>
#include <fstream>
#include<string.h>
#include <malloc.h>
#include <time.h>
#include "Heap.h"
#include <strings.h>
#include "../common.h"

namespace HeapGUI{

int initHeap();
};

int HeapGUI::initHeap(){

	enum database{EXIT, CREATE, OPEN,INSERT, DELETEDATA, UPDATEDATA, GETDATA, GETALLDATA};

		Heap hp;
		Status st = ok;
		int file = -1;
		char* data = "hello world";
		char* data1 = "this is swapnil mishra";
		char* data2 = "i m in iiitb";
		char* data3 = "doing M.Tech from here";
		char* data4 = "this place is kool";
		char* data5 = "new one";
		char* update = "updated data";
		char* rec;
		PageId pid;
		int input = -1;
		printf("********************************************Menu for the Database******************************************************************\n\n\n");


	while(true){
		printf("Press the numbers and then enter to do the operations\n\n");
		printf("1 - Create Heap File\n");
		printf("2 - Open Heap File\n");
		printf("3 - Insert Data\n");
		printf("4 - Delete Data\n");
		printf("5 - Update Data\n");
		printf("6 - Get Data\n");
		printf("7 - Get All Data\n\n");
		printf("0 - Exit Program\n");
		printf("Enter the choice : ");
		scanf("%d",&input);
		switch(input){

		case EXIT:
			cout << "Program Terminated\n";
			return 0;

		case CREATE: //TODO here there should be a creation of heap file the starting header file will be passed from upper UI
//			hp.initDb(0);
			file = hp.initFirstDpPage();
			cout<<"File Created In Page No.: "<<file;
			hp.exitDb();
			break;
		case OPEN:
			cout<<"Enter The Heap Page No. to Open: ";
			cin>>file;
			break;

		case INSERT:
			if(file == -1)
			{
				cout<<"Open/Create file first";
				break;
			}
//			hp.initDb(1);
			int count;
			printf("Enter the Number of records you want to enter in multiple of 6 :");
			scanf("%d",&count);
			for (int iter = 0; iter <count; iter++) {
					hp.insertRecord(data, file);
					hp.insertRecord(data1, file);
					hp.insertRecord(data2, file);
					hp.insertRecord(data3, file);
					hp.insertRecord(data4, file);
					hp.insertRecord(data5, file);
				}
			hp.exitDb();
		break;

		case DELETEDATA:
			if(file == -1)
			{
				cout<<"Open/Create file first";
				break;
			}
//			hp.initDb(1);
			printf("\nEnter the Record Id of the record you want to delete ---->\n\n");
			printf("Enter Page Number: ");
			scanf("%d",&pid.pageno);
			printf("Enter Slot Number: ");
			scanf("%d",&pid.slotno);
			hp.deleteRecord(pid);
			hp.exitDb();
		break;

		case UPDATEDATA:
			if(file == -1)
			{
				cout<<"Open/Create file first";
				break;
			}
//			hp.initDb(1);
			printf("\nEnter the Record Id of the record you want to update ---->\n\n");
			printf("Enter Page Number: ");
			scanf("%d",&pid.pageno);
			printf("Enter Slot Number: ");
			scanf("%d",&pid.slotno);
			hp.updateRecord(pid, update);
			hp.exitDb();
		break;

		case GETDATA:
			if(file == -1)
			{
				cout<<"Open/Create file first";
				break;
			}
//			hp.initDb(1);
			printf("\nEnter the Record Id of the record you want to get ---->\n\n");
			printf("Enter Page Number: ");
			scanf("%d",&pid.pageno);
			printf("Enter Slot Number: ");
			scanf("%d",&pid.slotno);
			rec = hp.getRecord(pid);

			if(rec == NULL) cout << "no record exist at this page and slot \n";
			else if(rec != NULL){
				cout << "\n\nRecord found :\t";
				 for(unsigned int iter=0; iter<strlen(rec); iter++){
					 cout << *(rec + iter) ;
				 }
			}
			cout << "\n\n\n";
			hp.exitDb();
		break;

		case GETALLDATA:
			if(file == -1)
			{
				cout<<"Open/Create file first";
				break;
			}
			//hp.initDb(1);
			hp.openScan(file);
			hp.exitDb();
		break;

		default:
			cout << "invalid input\n";
		break;
		};

//		printf("0 - Exit Program\n");
//		printf("1 - Create Database\n");
//		printf("2 - Insert Data\n");
//		printf("3 - Delete Data\n");
//		printf("4 - Update Data\n");
//		printf("5 - Get Data\n");
//		printf("6 - Get All Data\n\n");
//		printf("Enter the choice : ");
//		scanf("%d",&input);
	};
}





